﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Game.Facade;
using Game.Kernel;
using Game.Utils;
using Game.Entity.Accounts;
using Game.Entity.Treasure;

namespace Game.Card.Menu
{
    public partial class Exchange : AdminPage
    {
        protected int rateRoomCard = 1;   //房卡兑换游戏币比例
        protected void Page_Load(object sender, EventArgs e)
        {
            SiteMaster.Menu = 2;
            if(!IsPostBack)
            {
                //获取登录信息
                UserTicketInfo user = userTicket;

                //获取游戏币信息
                GameScoreInfo score = FacadeManage.aideTreasureFacade.GetTreasureInfo2(user.UserID);
                lbGold.Text = score != null ? score.Score.ToString() : "0";

                //获取房卡信息
                UserRoomCard card = FacadeManage.aideTreasureFacade.GetUserRoomCard(user.UserID);
                lbRoomCard.Text = card != null ? card.RoomCard.ToString() : "0";

                //获取兑换比例
                SystemStatusInfo ssi = FacadeManage.aideAccountsFacade.GetSystemStatusInfo(AppConfig.SystemConfigKey.AgentRoomCardExchRate.ToString());
                if(ssi != null)
                    rateRoomCard = ssi.StatusValue;
            }
        }

        /// <summary>
        /// 确定兑换
        /// </summary>
        protected void btnExch_Click(object sender, EventArgs e)
        {
            //获取兑换数据
            int exchCard = CtrlHelper.GetInt(txtCardCount, 0);
            string password = CtrlHelper.GetText(txtPassword);

            //数据验证
            if(exchCard <= 0)
            {
                ShowInfo("抱歉，兑换房卡数需大于或等于零");
                return;
            }
            if(string.IsNullOrEmpty(password))
            {
                ShowInfo("抱歉，银行密码不能为空");
                return;
            }

            //执行兑换操作
            Message msg = FacadeManage.aideTreasureFacade.ExchRoomCard(userTicket.UserID, TextEncrypt.EncryptPassword(password), exchCard, GameRequest.GetUserIP());
            if(msg.Success)
            {
                Response.Redirect("/Menu/ExchangeSuccess.aspx");
            }
            else
            {
                txtCardCount.Text = "0";
                ShowInfo(msg.Content);
            }
        }
    }
}